<template>
  <div class="tab-container">
    <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card">
      <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key">
        <keep-alive>
          <tab-pane v-if="activeName==item.key" :type="item.key" :list="list"@create="showCreatedTimes" />
        </keep-alive>
        <input v-model="uid" type="text" placeholder="请输入要删除的调度uid" class="input-item">
        <input v-model="tid" type="text" placeholder="请输入要删除的调度tid" class="input-item">
        <button class="btn" @click="Deletetask">delete</button>
      </el-tab-pane>
    </el-tabs>
    <input v-model="tid_modify" type="text" placeholder="请输入要修改的调度tid" class="input-item">
    <input v-model="uid_modify" type="text" placeholder="请输入要修改的调度uid" class="input-item">
    <button class="btn" @click="modify_task">sign</button>
    <input v-model="tid_finish" type="text"placeholder="请输入确认完成的任务tid">
    <button class="btn" @click="finish_task">finish</button>
  </div>
</template>

<script>
import TabPane from './components/TabPane'

import table_data from './components/table_data.vue'

export default {
  name: 'Tab',
  components: { TabPane, table_data },
  data() {
    return {
      tabMapOptions: [
      { label: 'USA', key: 'US' },
      ],
      activeName: 'US',
      createdTimes: 0,
      taskArray: [],
      list: [],
      uid: '',
      tid: '',
      uid_modify: '',
      tid_modify: '',
      tid_finish: ''
    }
  },
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },
  created() {
    var myHeaders = new Headers()
    myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
    myHeaders.append('Content-Type', 'application/json')
    myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))

    var raw = JSON.stringify({})

    var requestOptions = {
      method: 'POST',
      headers: myHeaders,
      body: raw,
      redirect: 'follow'
    }

    fetch('http://1.92.204.101:5000/task/auto_assign', requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error))
  },
  mounted() {
    var myHeaders = new Headers()
    myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
    myHeaders.append('Content-Type', 'application/json')
    myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))

    var raw = JSON.stringify({})

    var requestOptions = {
      method: 'POST',
      headers: myHeaders,
      body: raw,
      redirect: 'follow'
    }

    fetch('http://1.92.204.101:5000/task/auto_assign', requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error))
    var myHeaders = new Headers()
    myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
    myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))
    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      redirect: 'follow'
    }

    fetch('http://1.92.204.101:5000/task/get_user_statement', requestOptions)
      .then(response => response.text())
      .then(result => {
        var data = JSON.parse(result)
        this.taskArray = data
        this.list = this.taskArray.map(item => {
          return {
            id: item.uid,
            timestamp: item.assigned === 0 ? null : item.starttime,
            title: item.assigned === 0 ? null : item.task_placex.toString() + '  , ' + item.task_placey.toString(),
            author: item.name,
            pageviews: item.assigned
          }
        })
      })
      .catch(error => console.log('error', error))
  },
  created() {
    // init the default selected tab
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
    Deletetask() {
      var myHeaders = new Headers()
      myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
      myHeaders.append('Content-Type', 'application/json')
      myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))

      var raw = JSON.stringify({
        'tid': this.tid,
        'uid': this.uid
      })

      var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
      }

      fetch('http://1.92.204.101:5000/task/delete_assign_statement', requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error))
    },
    modify_task() {
      var myHeaders = new Headers()
      myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
      myHeaders.append('Content-Type', 'application/json')
      myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))

      const str = this.uid_modify
      const arr = str.split(/\s+/)
      console.log(arr)
      var raw = JSON.stringify({
        'uids': arr.map(Number),
        'tid': this.tid_modify
      })
      console.log(raw)
      var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
      }

      fetch('http://1.92.204.101:5000/task/modify_assign_statement', requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error))
    },
    finish_task() {
      var myHeaders = new Headers()
      myHeaders.append('User-Agent', 'Apifox/1.0.0 (https://apifox.com)')
      myHeaders.append('Content-Type', 'application/json')
      myHeaders.append('Authorization', 'Bearer ' + sessionStorage.getItem('token'))
      var raw = JSON.stringify({
        'tid': this.tid_finish
      })

      var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow'
      }

      fetch('http://1.92.204.101:5000/task/finish_task', requestOptions)
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error))
    }
  }
}
</script>

<style scoped>
  .tab-container {
    margin: 30px;
  }
</style>
